//
//  ByViewShopCarSettleToolBar.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/10/27.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByViewShopCarSettleToolBar.h"

@implementation ByViewShopCarSettleToolBar
{
    UIButton    *_commitButton;     // 结算
    UILabel     *_totalPriceLabel;  // 合计
}

- (instancetype)init {
    
    if (self = [super init]) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        [self setupUI];
    }
    return self;
}

#pragma mark - 创建UI
- (void)setupUI {

    // 线条
    UIView *line = [UIView new];
    line.backgroundColor = BY_LINE_COLOR;
    [self addSubview:line];
    
    
    // 结算按钮
    _commitButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _commitButton.backgroundColor = BY_RED_COLOR;
    _commitButton.titleLabel.font = FONT(16);
    [_commitButton setTitle:@"提交" forState:UIControlStateNormal];
    [_commitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_commitButton addTarget:self action:@selector(commit) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_commitButton];
    
    // 合计label
    _totalPriceLabel = [[UILabel alloc] init];
    _totalPriceLabel.adjustsFontSizeToFitWidth = YES;
    _totalPriceLabel.font = FONT(18);
    _totalPriceLabel.text = @"合计：￥0.00";
    _totalPriceLabel.textColor = BY_RED_COLOR;
    _totalPriceLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_totalPriceLabel];
    
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:_totalPriceLabel.text];
    [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:[_totalPriceLabel.text rangeOfString:@"合计："]];
    [attrStr addAttribute:NSFontAttributeName value:FONT(12) range:[_totalPriceLabel.text rangeOfString:@"￥"]];
    [attrStr addAttribute:NSFontAttributeName value:FONT(12) range:NSMakeRange(_totalPriceLabel.text.length - 2, 2)];
    _totalPriceLabel.attributedText = attrStr;
    
    // 布局
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(@0);
        make.height.equalTo(@1);
    }];
    [_commitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.equalTo(@0);
        make.width.equalTo(@98);
    }];
    [_totalPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_commitButton.mas_left).offset(-10);
        make.centerY.equalTo(_commitButton.mas_centerY);
        make.height.equalTo(@20);
    }];
}

- (void)setTotalPrice:(CGFloat)totalPrice {
    
    _totalPrice = totalPrice;
    
    _totalPriceLabel.text = [NSString stringWithFormat:@"合计：￥%.2lf", totalPrice];
    
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:_totalPriceLabel.text];
    [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:[_totalPriceLabel.text rangeOfString:@"合计："]];
    [attrStr addAttribute:NSFontAttributeName value:FONT(12) range:[_totalPriceLabel.text rangeOfString:@"￥"]];
    [attrStr addAttribute:NSFontAttributeName value:FONT(12) range:NSMakeRange(_totalPriceLabel.text.length - 2, 2)];
    _totalPriceLabel.attributedText = attrStr;
}


#pragma mark - 点击事件
// 结算
- (void)commit {
    
    if (self.commitBlock) {
        self.commitBlock();
    }
}
@end
